﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using Kinect;

namespace PresentationProject
{
    /// <summary>
    /// Interaction logic for TotalPage.xaml
    /// </summary>
    public partial class TotalPage : Page
    {
        private KinectController _kinect;   //声明KinectController对象
        
        
        public TotalPage()
        {
            InitializeComponent();

            iscaptured = false;

            _kinect = new KinectController(false);
            _kinect.outGR += new KinectController.outGestureRecognizedEventHandler(GestureRecoganized);
            _kinect.outzoom += new KinectController.zoomEventHandler(Zoom);
            _kinect.InthePosition += new KinectController.playerInPositionEventHandler(GoToTheOtherSide);
            current = 0;
            sbList = new List<Storyboard>();
            insertStoryboard();
            showStoryboard();
        }

        private void GoToTheOtherSide(object sender)
        {
            ClassControl.nextClass();
        }
        private void Zoom(object sender)
        {
            ClassControl.nextClass();
            _kinect.GetPlayerPostion = true;
        }
        private void showStoryboard()
        {
            if(current >=0 && current < sbList.Count)
                BeginStoryboard(sbList[current]);
        }

        private void nextStoryboard()
        {
            if (current >= 0 && current < sbList.Count)
            {
                ++current;
                showStoryboard();
            }
            else if (current == sbList.Count)
            {
                MessageBox.Show("已经没有了。");
            }
        }

        private void preStoryboard()
        {
            if (current > 0 && current <= sbList.Count)
            {
                --current;
                showStoryboard();
            }
            else if (current == 0)
            {
                MessageBox.Show("已经没有了。");
            }
        }

        private void insertStoryboard()
        {
            Storyboard sb0 = (Storyboard)FindResource("Storyboard0");
            Storyboard sb1 = (Storyboard)FindResource("Storyboard1");
            Storyboard sb2 = (Storyboard)FindResource("Storyboard2");
            Storyboard sb3 = (Storyboard)FindResource("Storyboard3");
            Storyboard sb4 = (Storyboard)FindResource("Storyboard4");
            Storyboard sb5 = (Storyboard)FindResource("Storyboard5");
            Storyboard sb6 = (Storyboard)FindResource("Storyboard6");
            Storyboard sb7 = (Storyboard)FindResource("Storyboard7");
            Storyboard sb8 = (Storyboard)FindResource("Storyboard8");
            Storyboard sb9 = (Storyboard)FindResource("Storyboard9");
            Storyboard sb10 = (Storyboard)FindResource("Storyboard10");
            Storyboard sb11 = (Storyboard)FindResource("Storyboard11");
            Storyboard sbLast = (Storyboard)FindResource("StoryboardLast");
            Storyboard csb1 = (Storyboard)FindResource("ClassStoryboard1");

            sbList.Add(sb0);
            sbList.Add(sb1);
            sbList.Add(sb2);
            sbList.Add(csb1);
            sbList.Add(sb3);
            sbList.Add(sb4);
            sbList.Add(sb5);
            sbList.Add(sb6);
            sbList.Add(sb7);
            //page9Control.RenderTransform.SetValue(ScaleTransform.CenterXProperty, 1.0);
            sbList.Add(sb8);
            sbList.Add(sb9);
            sbList.Add(sb10);
            sbList.Add(sb11);
            sbList.Add(sbLast);
        }

        private List<Storyboard> sbList;
        private int current;

        private void Page1_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta < 0)
            {
                nextStoryboard();
            }
            else
            {
                preStoryboard();
            }
        }


        public void GestureRecoganized(object sender, outGestureRecognizedEventArgs e)//处理函数
        {
            if (e.GID == "RightHandSwipeLeft")
                nextStoryboard();
            else if (e.GID == "LeftHandSwipeRight")
                preStoryboard();
        }

        private bool iscaptured;
        private Point previousPosition;

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            //iscaptured = true;
            previousPosition = e.GetPosition(this);
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);

            iscaptured = false;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (iscaptured)
            {
                Point currentPosition = e.GetPosition(this);

                //MessageBox.Show(previousPosition.ToString() + '\n' + currentPosition.ToString());

                if (currentPosition == previousPosition)
                    return;
                //Double orx = (Double)(this.RenderTransform.GetValue(TranslateTransform.XProperty));
                //Double ory = (Double)(this.RenderTransform.GetValue(TranslateTransform.YProperty));
                //this.RenderTransform.SetValue(TranslateTransform.XProperty, orx
                //    + (currentPosition.X - previousPosition.X)*10);
                //this.RenderTransform.SetValue(TranslateTransform.YProperty,  ory
                //    + (currentPosition.Y - previousPosition.Y)*10);
                //Double chx = (Double)(this.RenderTransform.GetValue(TranslateTransform.XProperty));
                //Double chy = (Double)(this.RenderTransform.GetValue(TranslateTransform.YProperty));

                //MessageBox.Show(orx.ToString() + ' ' + ory.ToString() + '\n' + chx.ToString() + ' '+ chy.ToString());


                Thickness tktmp = new Thickness();
                tktmp.Left = this.Margin.Left + currentPosition.X - previousPosition.X;
                tktmp.Top = this.Margin.Top + currentPosition.Y - previousPosition.Y;
                this.Margin = tktmp;

                previousPosition.X = currentPosition.X;
                previousPosition.Y = currentPosition.Y;
            }
        }
    }
}
